<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select size="small" @change="changeDate()" v-model="currentYear" style="width: 120px">
         <el-option v-for="item in yearList" :key="item" :value="item" ></el-option>
      </el-select>
      <el-select size="small" @change="changeDate()" v-model="currentMonth" style="width: 120px;margin-left:10px">
        <el-option v-for="item in 12" :key="item" :value="item" ></el-option>
      </el-select>
    </el-row>
    <el-calendar v-model="currentDate">
      <template
        slot="dateCell"
        slot-scope="{date, data}" class="content">
        <!-- date 每天的日期对象 -->
        <!-- data 是数据对象 isSelect type day -->
        <div class="date-content">
          <span class="text"> {{ data | getDay }}</span>
          <span class="rest" v-if="isWeek(date)">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  props: {
    // 设计startDate为开始时间；如果将来使用这个组件传入开始时间，则从那个时间开始；否则就是当前
    startDate: {
      type: Date,
      default: () => new Date()   // default 如果值为引用数据类型，则需要 用函数返回
    }
  },
  data() {
    return {
      currentMonth: null, // 当前月份
      currentYear: null, // 当前年份
      currentDate: null,
      yearList: []  // 年份列表  【  过去5年   到   5年之后】
    }
  },
  created () {
      this.currentMonth = this.startDate.getMonth() + 1 
      this.currentYear = this.startDate.getFullYear()
      this.yearList = Array.from(new Array(11),(item,index)=> this.currentYear + index - 5 ) 
      this.changeDate(this.startDate.getDate())
  },
  filters:{
    getDay({ day }){
      return day.split('-')[2]
    }
  },
  methods: {
    isWeek(date) {
      // getDate 获取到日期   getDay 获取礼拜几
      return [0,6].includes(date.getDay())
    },
    // 日期修改
    changeDate(d){
      const date = d || '1'
      const dateStr = this.currentYear +'-' + this.currentMonth + '-' + date
      // new Date('年份-月份-日')
      this.currentDate = new Date(dateStr)
    }
  },
}
</script>

<style lang="scss" scoped>
  ::v-deep .el-calendar-day {
    height: auto;
  }
  ::v-deep .el-calendar-table__row td,
  ::v-deep .el-calendar-table tr td:first-child,
  ::v-deep .el-calendar-table__row td.prev {
    border: none;
  }
  ::v-deep .is-today{
    color: #409EFF;
  }
  .date-content {
    height: 40px;
    text-align: center;
    line-height: 40px;
    font-size: 14px;
  }
  .date-content .rest {
    color: #fff;
    border-radius: 50%;
    background: rgb(250, 124, 77);
    width: 20px;
    height: 20px;
    line-height: 20px;
    display: inline-block;
    font-size: 12px;
    margin-left: 10px;
  }
  .date-content .text {
    width: 20px;
    height: 20px;
    line-height: 20px;
    display: inline-block;
  }
  ::v-deep .el-calendar-table td.is-selected .text {
    background: #409eff;
    color: #fff;
    border-radius: 50%;
  }
  ::v-deep .el-calendar__header {
    display: none;
  }
</style>